package com.saunter.controller;

import com.saunter.model.User;
import com.saunter.service.SalaryService;
import com.saunter.service.user.UserService;
import com.saunter.utils.CookieUtils;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"用户"})
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SalaryService salaryService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Map<String, Object> login(HttpServletResponse response, @RequestParam("id_num") String id_num, @RequestParam("passwd") String passwd) {
        Map<String, String> res_login = userService.login(id_num, passwd);
        if (null != res_login.get("err_code")) {
            throw exceptionManager.create("Lg001");
        } else {
            String error_code = res_login.get("err_code");
            if (null == error_code) {
                String jwt_token = res_login.get("sid");
                if (null != jwt_token) {
                    CookieUtils.addUserCookie(response, "sid", jwt_token);
                    return Response.success("登录成功");
                } else {
                    throw exceptionManager.create("Se001");
                }
            } else {
                throw exceptionManager.create(error_code);
            }
        }
    }

    @ApiOperation("登出")
    @GetMapping("logout")
    public Map<String, Object> logout(HttpServletResponse response) {
        userService.removeCookie(response);
        return Response.success("登出成功");
    }

    @ApiOperation("检测是否登录")
    @GetMapping("/isLogin")
    public Map<String, Object> isLogin() {
        return Response.success("已登录");
    }

    @ApiOperation("查询自身数据")
    @GetMapping("/getRecord/{page_no}/{page_size}")
    public Map<String, Object> getRecord(@PathVariable("page_no") int page_no, @PathVariable("page_size") int page_size, @ApiParam(hidden = true) @RequestParam("system_append_id_num") String id_num,
                                         @ApiParam("搜索的结束日期") @RequestParam(value = "date_start", required = false) String date_start, @ApiParam("搜索的结束日期") @RequestParam(value = "date_end", required = false) String date_end) {
        return Response.success(userService.getSalary(page_no, page_size, id_num, date_start, date_end));
    }

    @ApiOperation("导出自身数据")
    @GetMapping("/exportRecord")
    public void exportSalary(HttpServletResponse response, @ApiParam(hidden = true) @RequestParam("system_append_id_num") String id_num,
                             @ApiParam("搜索的结束日期") @RequestParam(value = "date_start", required = false) String date_start, @ApiParam("搜索的结束日期") @RequestParam(value = "date_end", required = false) String date_end) throws Exception {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("id_num", id_num);
        filters.put("date_start", date_start);
        filters.put("date_end", date_end);
        filters.put("status", 1);
        salaryService.exportRecords(response, filters, false);
    }

    @ApiOperation(value = "获取可用的数据标题", notes = "只能查询到用户权限下的标题")
    @GetMapping("/getSalaryKey")
    public Map<String, Object> getSalaryKey() {
        return Response.success(userService.getSalaryKey());
    }

    @ApiOperation(value = "修改自身信息", notes = "修改密码后需要重新登录")
    @PostMapping("/editUser")
    public Map<String, Object> editUser(HttpServletRequest request, HttpServletResponse response, @ApiParam(hidden = true) @RequestParam("system_append_id_num") String id_num,
                                        @RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "new_passwd", required = false) String new_passwd, @RequestParam(value = "old_passwd", required = false) String old_passwd, @RequestParam(value = "email", required = false) String email) {
        boolean modify_passwd = null != new_passwd && !"".equals(new_passwd);
        if (modify_passwd) {
            if (!userService.checkPasswd(id_num, old_passwd)) {
                throw exceptionManager.create("Lg002");
            }
        }
        if (1 == userService.editUser(new User(id_num, new_passwd, null, null, null, null, phone, email, null, null, null))) {
            if (modify_passwd) {
                userService.removeCookie(response);
            }
            return Response.success("修改成功");
        } else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取自身信息")
    @GetMapping("/userInfo")
    public Map<String, Object> getUserInfo(@ApiParam(hidden = true) @RequestParam("system_append_id_num") String id_num) {
        Map<String, Object> user_info = userService.getSelf(id_num);
        if (null == user_info) {
            throw exceptionManager.create("Lg001");
        }
        return Response.success(user_info);
    }
}